#!/bin/bash

# 确保脚本在错误时停止
set -e

echo "开始可靠部署Todo应用..."

# 配置Docker使用多个国内镜像源
if [ ! -f /etc/docker/daemon.json ] || ! grep -q "registry-mirrors" /etc/docker/daemon.json; then
    echo "配置Docker使用国内镜像源..."
    mkdir -p /etc/docker
    cat > /etc/docker/daemon.json <<EOF
{
  "registry-mirrors": [
    "https://docker.mirrors.ustc.edu.cn",
    "https://hub-mirror.c.163.com",
    "https://mirror.baidubce.com"
  ]
}
EOF
    systemctl daemon-reload
    systemctl restart docker
    echo "Docker已配置为使用国内镜像源"
    # 等待Docker重启完成
    sleep 5
fi

# 预先拉取所需镜像
echo "预先拉取所需镜像..."

# 尝试使用不同的镜像源拉取镜像
echo "尝试从官方源拉取镜像..."
docker pull python:3.9-slim || echo "从官方源拉取python镜像失败，尝试其他源"
docker pull node:18-alpine || echo "从官方源拉取node镜像失败，尝试其他源"
docker pull nginx:alpine || echo "从官方源拉取nginx镜像失败，尝试其他源"

# 如果官方源失败，尝试使用中科大镜像源
if ! docker images | grep -q "python.*3.9-slim"; then
    echo "尝试从中科大镜像源拉取python镜像..."
    docker pull docker.mirrors.ustc.edu.cn/library/python:3.9-slim || echo "从中科大源拉取python镜像失败"
    # 如果成功拉取，给镜像打标签
    if docker images | grep -q "docker.mirrors.ustc.edu.cn/library/python.*3.9-slim"; then
        docker tag docker.mirrors.ustc.edu.cn/library/python:3.9-slim python:3.9-slim
    fi
fi

# 如果中科大源失败，尝试使用阿里云镜像源
if ! docker images | grep -q "python.*3.9-slim"; then
    echo "尝试从阿里云镜像源拉取python镜像..."
    docker pull registry.cn-hangzhou.aliyuncs.com/library-base/python:3.9-slim || echo "从阿里云源拉取python镜像失败"
    # 如果成功拉取，给镜像打标签
    if docker images | grep -q "registry.cn-hangzhou.aliyuncs.com/library-base/python.*3.9-slim"; then
        docker tag registry.cn-hangzhou.aliyuncs.com/library-base/python:3.9-slim python:3.9-slim
    fi
fi

if ! docker images | grep -q "node.*18-alpine"; then
    echo "尝试从中科大镜像源拉取node镜像..."
    docker pull docker.mirrors.ustc.edu.cn/library/node:18-alpine || echo "从中科大源拉取node镜像失败"
    # 如果成功拉取，给镜像打标签
    if docker images | grep -q "docker.mirrors.ustc.edu.cn/library/node.*18-alpine"; then
        docker tag docker.mirrors.ustc.edu.cn/library/node:18-alpine node:18-alpine
    fi
fi

# 如果中科大源失败，尝试使用阿里云镜像源
if ! docker images | grep -q "node.*18-alpine"; then
    echo "尝试从阿里云镜像源拉取node镜像..."
    docker pull registry.cn-hangzhou.aliyuncs.com/library-base/node:18-alpine || echo "从阿里云源拉取node镜像失败"
    # 如果成功拉取，给镜像打标签
    if docker images | grep -q "registry.cn-hangzhou.aliyuncs.com/library-base/node.*18-alpine"; then
        docker tag registry.cn-hangzhou.aliyuncs.com/library-base/node:18-alpine node:18-alpine
    fi
fi

if ! docker images | grep -q "nginx.*alpine"; then
    echo "尝试从中科大镜像源拉取nginx镜像..."
    docker pull docker.mirrors.ustc.edu.cn/library/nginx:alpine || echo "从中科大源拉取nginx镜像失败"
    # 如果成功拉取，给镜像打标签
    if docker images | grep -q "docker.mirrors.ustc.edu.cn/library/nginx.*alpine"; then
        docker tag docker.mirrors.ustc.edu.cn/library/nginx:alpine nginx:alpine
    fi
fi

# 如果中科大源失败，尝试使用阿里云镜像源
if ! docker images | grep -q "nginx.*alpine"; then
    echo "尝试从阿里云镜像源拉取nginx镜像..."
    docker pull registry.cn-hangzhou.aliyuncs.com/library-base/nginx:alpine || echo "从阿里云源拉取nginx镜像失败"
    # 如果成功拉取，给镜像打标签
    if docker images | grep -q "registry.cn-hangzhou.aliyuncs.com/library-base/nginx.*alpine"; then
        docker tag registry.cn-hangzhou.aliyuncs.com/library-base/nginx:alpine nginx:alpine
    fi
fi

# 检查是否成功拉取了所有镜像
echo "检查镜像拉取状态:"
docker images | grep -E 'python|node|nginx'

# 生成随机密钥
if grep -q "your_secret_key_here" docker-compose.yml; then
    SECRET_KEY=$(openssl rand -hex 16)
    echo "生成的密钥: $SECRET_KEY"
    sed -i "s/your_secret_key_here/$SECRET_KEY/g" docker-compose.yml
fi

# 增加超时时间
export DOCKER_CLIENT_TIMEOUT=300
export COMPOSE_HTTP_TIMEOUT=300

# 构建并启动容器
echo "构建并启动Docker容器..."
docker-compose up -d --build

echo "部署完成！应用现在应该在http://localhost或您的服务器IP上运行"
